﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel;

public partial class Add_Subscription : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();
    Subscription subscription = new Subscription();


    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.QueryString["Id"] != null)
            {
                lblHeader.Text = "Edit Subscription";
                BindData();
            }
            else
            {
                lblHeader.Text = "Add Subscription";
            }
        }
    }
    #endregion

    #region save  and clear
    protected void Save(object sender, EventArgs e)
    {
        Subscription subscription = new Subscription();
        try
        {
            subscription = new Subscription(_dataObject);
            BuildObject(subscription);
            subscription.GetSubscriptionByName();
            if (Request.QueryString["Id"] != null)
            {
                if (subscription.Id != Convert.ToInt32(Request.QueryString["Id"]) && subscription.Id != 0)
                {
                    //ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('PlanName is already exist..')", true);
                    divMessage.InnerHtml = "<strong>Error!</strong> Plan-Name already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger");
                    return;
                }
                BuildObject(subscription);
                subscription.Id = Convert.ToInt32(Request.QueryString["Id"]);
                subscription.UpdatedDate = DateTime.Now;
                subscription.IsDeleted = false;
                subscription.Update();
                divMessage.InnerHtml = "<strong>Success!</strong> Subscription updated successfully.";
                divMessage.Attributes.Add("class", "alert alert-success");
             
            }
            else
            {
                if (subscription.Id > 0)
                {

                    divMessage.InnerHtml = "<strong>Error!</strong> Subscription already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger");
                    return;
                }
                BuildObject(subscription);
                subscription.CreatedDate = System.DateTime.Now;
                subscription.Create();

                divMessage.InnerHtml = "<strong>Success!</strong> Subscription added successfully.";
                divMessage.Attributes.Add("class", "alert alert-success");
                ClearControls();
            }
          

        }
        catch (Exception ex)
        {
        }
    }

    private void ClearControls()
    {
        txtAmount.Text = string.Empty;
        txtDuration.Text = string.Empty;
        txtSubscriptionName.Text = string.Empty;
        txtDescription.Text = string.Empty;

    }
    #endregion

    #region binddata
    private void BindData()
    {
        subscription = new Subscription(_dataObject);
        subscription.Id = Convert.ToInt32(Request.QueryString["Id"]);
        subscription.GetSubscriptionById();
        txtSubscriptionName.Text = subscription.PlanName;
        txtAmount.Text = subscription.Amount.ToString();
        txtDuration.Text = subscription.Duration.ToString();
        txtDescription.Text = subscription.Description;

        //can't edit below fields 
        txtAmount.Enabled = false;
        txtDuration.Enabled = false;
        txtSubscriptionName.Enabled = false;

    }
    private void BuildObject(Subscription subscription)
    {
        subscription.PlanName = txtSubscriptionName.Text;
        subscription.Amount = Convert.ToDecimal(txtAmount.Text);
        subscription.Duration = Convert.ToInt32(txtDuration.Text);
        subscription.Description = txtDescription.Text;
    }
   
    #endregion
}